//
//  NSDate+Extension.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/20.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import Foundation

extension NSDate{
   //MARK:--- 将新浪微博的日期格式字符串生成一个NSDate
    class func sinaDate(str:String)->NSDate?{
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy" //注意：<##>Tue Sep 15 17:30:29 +0800 2014
        return df.dateFromString(str)
    }
    //MARK:--- 返回日期的表述
    var dateDescription:String{
        let canlender = NSCalendar.currentCalendar()//获取当前日历对象
        if canlender.isDateInToday(self){
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            if delta < 60{
                return "刚刚"
            }
            if delta < 3600{
                return "\(delta/60)分钟前"
            }
            return "\(delta/3600)小时前"
        }
        var fmtString  = " HH:mm"//处理今天外的其他日期
        if canlender.isDateInYesterday(self){
            fmtString = "昨天" + fmtString
        }else{
            fmtString = "MM-dd" + fmtString
            
            let coms = canlender.components(.Year, fromDate: self, toDate: NSDate(), options: [])
            
            if coms.year > 0 {
                fmtString = "yyyy-" + fmtString
            }
        }
        let df = NSDateFormatter()//根据格式字符串，生成对应日期字符串
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = fmtString
        return df.stringFromDate(self)
    }
}
    